package calorie.com.calorie.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.event.BusFactory;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.adapter.AccelerateAdapter;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.AccelerateContact;
import calorie.com.calorie.contact.GetAssetsContact;
import calorie.com.calorie.eventbus.EventImpl;
import calorie.com.calorie.response.AccelerateResponce;
import calorie.com.calorie.response.AssertInfomationResponce;
import calorie.com.calorie.response.BuyAccelerateResponce;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.response.UserAssets;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;


/**
 * 加速
 */
public class AccelerateActivity extends MvpActivity<AccelerateContact.AcceleratePresent> implements AccelerateContact.IAccelerateView, GetAssetsContact.IgetAssets {
    @BindView(R.id.tv_current_multiple)
    TextView tvCurrentMultiple;
    @BindView(R.id.rv)
    RecyclerView rv;
    List<AccelerateResponce.DataBean> mList;
    AccelerateAdapter mAdapter;
    GetAssetsContact.GetAssetsPresent assetsPresent;
    Double calPrice;
    Double vitPrice;
    @BindView(R.id.tv_accelerate_immediately)
    public View tv_accelerate_immediately;
    @BindView(R.id.no_accelerate_tx)
    TextView noAccelerateTx;
    @BindView(R.id.no_accelerate_lay)
    LinearLayout noAccelerateLay;

    @Override
    public void initData(Bundle savedInstanceState) {
        mList = new ArrayList<>();
        mAdapter = new AccelerateAdapter(R.layout.recy_item_accelerate, mList);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);

        if (StaticValue.userAssets != null) {
            tvCurrentMultiple.setText("x" + StaticValue.userAssets.getSpeed() + "倍速");
        }

        mPresent.queryAccelerateList(context);

    }

    @Override
    public void initBeforView(Bundle savedInstanceState) {
        super.initBeforView(savedInstanceState);
        assetsPresent = new GetAssetsContact.GetAssetsPresent();
        assetsPresent.attach(this);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_accelerate;
    }

    @OnClick(R.id.tv_accelerate_immediately)
    public void onViewClicked() {
        tv_accelerate_immediately.setEnabled(false);
        mPresent.getPrice();//先获取服务器价格


    }

    AlertDialog dealDialog = null;

    private void showDealDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = psw.getPsw();
                    if (dealPwd == null || dealPwd.equals("")) {
                        ToastUtils.showLongToast(context, "请输入密码");
                        return;
                    }
                    mPresent.buyAccelerate(context, dealPwd);
                    if (dealDialog != null)
                        dealDialog.dismiss();
                    dealDialog = null;
                }).setOnClickListener(R.id.tv_cancle, v -> {
                    if (dealDialog != null)
                        dealDialog.dismiss();
                    dealDialog = null;
                    if (buyDialog != null)
                        buyDialog.dismiss();
                    buyDialog = null;
                });
        if (dealDialog == null) {
            dealDialog = builder.create();
        }
        if (!dealDialog.isShowing()) {
            dealDialog.show();
        }
    }

    AlertDialog buyDialog = null;

    private void showBuyDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_buy_accelerate)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    showDealDialog();
                    buyDialog.dismiss();
                    buyDialog = null;
                }).setOnClickListener(R.id.tv_cancle, v -> {
                    buyDialog.dismiss();
                    buyDialog = null;
                    noAccelerateTx.setTextColor(getResources().getColor(R.color.common_black));
                });
        buyDialog = builder.create();
        buyDialog.show();
        TextView cal = buyDialog.getView(R.id.tv_cal);
        TextView vit = buyDialog.getView(R.id.tv_vit);
//        cal.setText("CAL:" + calPrice);
        cal.setText("CAL:" + 5000);
        vit.setText("VIT:" + vitPrice);


    }

    @Override
    public void getBuyAccelerateData(Response<BuyAccelerateResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "购买加速成功");
            assetsPresent.getAssets(context);
            mPresent.queryAccelerateList(context);
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getAccelerateList(Response<AccelerateResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<AccelerateResponce.DataBean> data = response.body().getData();
            if (!ObjectUtils.isEmpty(data)) {//有加速包时隐藏提示文字
                noAccelerateLay.setVisibility(View.GONE);
                mList.clear();
                mList.addAll(data);
                mAdapter.notifyDataSetChanged();
                rv.setVisibility(View.VISIBLE);
            } else {//无加速包时隐藏列表
                rv.setVisibility(View.GONE);
                noAccelerateLay.setVisibility(View.VISIBLE);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected AccelerateContact.AcceleratePresent createPresent() {
        return new AccelerateContact.AcceleratePresent();
    }

    @Override
    public void getAssetsResult(Response<AssertInfomationResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            Gson gs = new Gson();
            StaticValue.userAssets = gs.fromJson(gs.toJson(response.body().getData()), UserAssets.class);//保存用户资产信息
            BusFactory.getBus().post(new EventImpl.BuyAccelerateEvent());

            AssertInfomationResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            tvCurrentMultiple.setText("x" + data.getSpeed() + "倍速");
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }


    @Override
    public void getPriceResult(Response<ResultDTO> response) {
        int code = response.code();
        String toJson = new Gson().toJson(response.body().getData());
        Map<String, Double> map = new Gson().fromJson(toJson, Map.class);
        tv_accelerate_immediately.setEnabled(true);
        if (code == 200) {//如果获取价格成功，显示对话框
            calPrice = map.get("cal");
            vitPrice = map.get("激活券");
            showBuyDialog();
            noAccelerateTx.setTextColor(getResources().getColor(R.color.yellow_buy));

        }
    }

}
